package spring.cloud.tasks.executor.name_space.executor.task.task_item.task_item_execute_info;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.TreeCacheListener;
import spring.cloud.tasks.executor.utils.LogUtils;

@Slf4j
@Setter
@Getter
class TaskItemExecuteInfoNeedReportListener extends TreeCacheListener {
    private final TaskItemExecuteInfoNeedReportListenerHolder needReportListenerHolder;

    public TaskItemExecuteInfoNeedReportListener(TaskItemExecuteInfoNeedReportListenerHolder controlListenerHolder) {
        this.needReportListenerHolder = controlListenerHolder;
    }

    @Override
    protected void dataChanged(CuratorFramework curatorFramework, String path, TreeCacheEvent treeCacheEvent) {
        if (needReportListenerHolder.isShutdown) {
            return;
        }
        if (TaskItemExecuteInfoNeedReportNode.isReportPath(needReportListenerHolder.taskId, path) && (TreeCacheEvent.Type.NODE_ADDED == treeCacheEvent.getType() || TreeCacheEvent.Type.NODE_UPDATED == treeCacheEvent.getType())) {
            LogUtils.info(log, needReportListenerHolder.taskId, "{} received report event from console, start to flush data to zk.", needReportListenerHolder.taskId);
            //needReportListenerHolder.taskItemExecuteInfoService.report();
        }
    }
}
